package springboot.study.project.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import springboot.study.project.common.Result;
import springboot.study.project.model.Dormitory;
import springboot.study.project.service.DormitoryService;
import springboot.study.project.util.common.ResultUtil;

import java.util.List;
import java.util.Map;

/**
 * @ClassName: DormitoryController
 * @Description: 宿舍控制层
 * @Author: liu-hao
 * @Date: 2021-04-12 14:26
 * @Version: 1.0
 **/
@RestController
@RequestMapping("/dormitory")
public class DormitoryController {
    @Autowired
    DormitoryService dormitoryService;

    @RequestMapping("/addDormitory")
    public Result addDormitory(@RequestBody Dormitory dormitory) {
        boolean result = dormitoryService.addDormitory(dormitory);
        if (!result) {
            return ResultUtil.failure("添加宿舍失败！");
        }
        return ResultUtil.success();
    }

    @RequestMapping("/removeDormitory")
    public Result removeDormitory(@RequestParam(name = "dormitoryId") String dormitoryId) {
        boolean result = dormitoryService.removeDormitory(dormitoryId);
        if (!result) {
            return ResultUtil.failure("删除宿舍失败！");
        }
        return ResultUtil.success();
    }

    @RequestMapping("/modifyDormitory")
    public Result modifyDormitory(@RequestBody Dormitory dormitory) {
        boolean result = dormitoryService.modifyDormitory(dormitory);
        if (!result) {
            return ResultUtil.failure("添加宿舍失败！");
        }
        return ResultUtil.success();
    }

    @RequestMapping("/getAllDormitory")
    Result getAllDormitory(@RequestParam("dormitoryId") String dormitoryId) {
        List<Dormitory> allDormitory = dormitoryService.getAllDormitory(dormitoryId);
        if (allDormitory == null) {
            return ResultUtil.failure("未查询到宿舍！");
        }
        return ResultUtil.success(allDormitory);
    }

    @RequestMapping("/getBulletins")
    public Map<String, Object> findDormitory(@RequestParam(name = "dormitoryName") String dormitoryName, // 宿舍名称
                                             @RequestParam(name = "page", defaultValue = "1") int pageNum,
                                             @RequestParam(name = "limit", defaultValue = "10") int pageSize) {
        int offset = (pageNum - 1) * pageSize;
        Map<String, Object> hotBulletins = dormitoryService.findDormitory(dormitoryName, offset, pageSize);
        return hotBulletins;
    }
}
